package com.rinvay.utils;

import java.util.Hashtable;

public class MIMEManager
{
    private static Hashtable<String, String> mimeTable = new Hashtable<String, String>();
    static {
        // Image type
        mimeTable.put("jpg", "image/jpeg");
        mimeTable.put("jpeg", "image/jpeg");
        mimeTable.put("png", "image/png");
        mimeTable.put("bmp", "image/bmp");

        // Video type
        mimeTable.put("mp4", "video/mpeg4");
        mimeTable.put("flv", "video/mpeg4");
        mimeTable.put("3gp", "video/3gpp");
        mimeTable.put("wmv", "video/mpeg");
        mimeTable.put("mpeg", "video/mpeg");
        mimeTable.put("mpg", "video/mpeg");

        //Audio type
        mimeTable.put("mp3", "audio/mp3");
        mimeTable.put("wma", "audio/mp3");
    }

    /**
     * 获取扩展名对应的mime
     * @param ext
     * @return
     */
    public static String getMimeType(String ext) {
        return mimeTable.get(ext.toLowerCase());
    }

    /**
     * 获取文件扩展名
     * 
     * @param url URL
     * @return Extension
     */
    public static String getFileExtension(String url) {
        if ((url != null) && (url.indexOf('.') != -1)) {
            return url.substring(url.lastIndexOf('.') + 1);
        }

        return "";
    }

    /**
     * 是否Image类型
     * 
     * @param mime MIME type
     * @return Boolean
     */
    public static boolean isImageType(String mime) {
        if (mime.toLowerCase().startsWith("image/")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否Video类型
     * 
     * @param mime MIME type
     * @return Boolean
     */
    public static boolean isVideoType(String mime) {
        if (mime.toLowerCase().startsWith("video/")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否Audio 类型
     * 
     * @param mime MIME type
     * @return Boolean
     */
    public static boolean isAudioType(String mime) {
        if (mime.toLowerCase().startsWith("audio/")) {
            return true;
        } else {
            return false;
        }
    }

}
